import Vue from 'vue';
import Vuex from 'vuex';
import { api } from '../Axios';
import _ from 'lodash';

Vue.use(Vuex);

export default new Vuex.Store({
  modules: {},
  state: {
    TOKEN: null,
    msg: { name: 'a123' },
    // prettier-ignore
    equip: {
      头部: null,
      躯干: null,
      手部: null,
      脚部: null,
      右手: null,
      左手: null,
      腰带: null,
      戒指: null,
      项链: null,
    }
  },
  getters: {
    // getValue: (state) => (key) => {
    //   return state[key];
    // },
  },
  mutations: {
    setValue: (state, obj) => {
      state[obj.key] = obj.value;
    },
    removeEquip: (state, slot) => {
      state[slot] = null;
    },
    replaceEquipment: (state, { set, slot }) => {
      state[slot] = set;

      //   replaceEquipment(slot, newItem) {
      //     if (slot in this.equipment) {
      //         this.equipment[slot] = newItem;
      //         console.log(`${newItem.name} has been equipped to ${slot}.`);
      //     } else {
      //         console.log(`Invalid equipment slot: ${slot}`);
      //     }
      // }
    },
  },
  actions: {
    fetchCusInfo: (ctx, payload) => {},
    卸下某位置的装备: (ctx, slot) => {
      let p = {};
      let d = {};
      return api
        .卸下某位置的装备(p, d)
        .then((res) => {
          ctx.commit('removeEquip');
        })
        .catch((err) => {
          console.error(err);
        })
        .finally((_) => {});
    },
    装上某装备到某位置: (ctx, { set, slot }) => {
      let p = {};
      let d = {};
      return api
        .装上某装备到某位置(p, d)
        .then((res) => {
          ctx.commit('replaceEquipment');
        })
        .catch((err) => {
          console.error(err);
        })
        .finally((_) => {});
    },
  },
});
